<?php

namespace Route;


use app\Index;
use App\LogController;
use App\LoginController;
use App\RegisterController;

class Route
{
    public $request;

    function __construct($request)
    {
        $this->request = $request;
    }

    function route()
    {

        if($this->request['uri'] == '/'){
            $this->access(Index::class, 'get');
            return;
        }
        $route = include ROOT_PATH . '/' . '/route.php';
        foreach ($route as $val) {
            foreach ($val as $uri => $class) {
                if (('/' . $uri) == $this->request['uri']) {
                    $this->access($class, $this->request['method']);
                    return;
                }
            }
        }

        echo '404 not found';
    }

    //访问控制器
    function access($class, $method)
    {
        try {
            $object = new $class();
            $object->$method($this->request);
        } catch (\Error $e) {
            echo 'this uri has not ' . $method . ' method';
        }

    }

}